<?php
/**
 * RedXenon Framework
 * 
 * LICENSE
 * 
 * This source file is subject to the New BSD license that is bundled
 * with this package in the file LICENSE.txt
 * A copy of this license can be found in the world-wide-web at this URL:
 * http://www.redxenon.com/license/new-bsd-license.txt * 
 */

/**
 * RedXenon Form (RXForm.php)
 * Defines methods to create HTML forms
 * 
 * @category RedXenon
 * @package RedXenon_Form
 * @copyright Copyright (c) 2011 Red Xenon (http://www.redxenon.com)
 * @version $Id: RXForm.php 1.0 2011-05-02 $ 
 */
class RXForm {

	/**
	 * Form name property
	 * @var String
	 */
	private $name;
	
	/**
	 * Form id property
	 * @var String
	 */
	private $id;
	
	/**
	 * Action to be executed on form submit
	 * @var String
	 */
	private $action;
	
	/**
	 * Sending vars method
	 * @var String
	 */
	private $method;
	
	/**
	 * Set of elements included in the form
	 * @var String
	 */
	private $elements;
	
	/**
	 * Form instances constructor
	 * @param String $method
	 * @param String $action
	 * @param String $id
	 * @param String $name
	 */
	public function __construct($method = "POST", $action = NULL, $id = NULL, $name = NULL){
		$this->name = $name;
		$this->id = $id;
		$this->method = $method;
		$this->action = $action;
		$this->elements = array();
	}
	
	/**
	 * Sets the name property
	 * @param String $name
	 */
	public function setName($name){
		$this->name = $name;
	}
	
	/**
	 * Sets the id property
	 * @param String $id
	 */
	public function setId($id){
		$this->id = $id;
	}
	
	/**
	 * Sets the form method
	 * @param String $method
	 */
	public function setMethod($method){
		$this->method = $method;
	}
	
	/**
	 * Setrs the form action
	 * @param String $action
	 */
	public function setAction($action){
		$this->action = $action;
	}
	
	/**
	 * Adds an element to the form
	 * @param String $element
	 */	
	public function addElement($element, $label = NULL){
		array_push($this->elements, array("label" => $label, "element" => $element));
	}
	
	/**
	 * Process the form elements and its properties and 
	 * shows an output in paragraphs or a two columns table
	 * @param String $style
	 */
	public function getOutput($style = NULL){
		$ret = "<form";
		
		if(isset($this->action))
			$ret .= ' action="'.$this->action.'"';
		if(isset($this->method))
			$ret .= ' method="'.$this->method.'"';
		if(isset($this->name))
			$ret .= ' name="'.$this->name.'"';
		if(isset($this->id))
			$ret .= ' id="'.$this->id.'"';
		
		$ret .= ">";
		
		switch(strtoupper($style)){
			case "TABLE": $ret .= $this->getTableOutput(); break;
			case "PARAGRAPH": $ret .= $this->getParagraphOutput(); break;
			default: $ret .= $this->getParagraphOutput(); break;
		}
		
		$ret .= "</form>";
	}
	
	/**
	 * Process the form elements and represents a table
	 * @return String
	 */
	public function getTableOutput(){
		$ret = '<table border="0" cellspacing="0" cellpadding="0">\n';
		foreach($this->elements as $element){
			if(!empty($element["label"])){
				$ret .= '<tr><td>'.$element["label"].'</td><td>'.$element["element"]->getOutput().'</td></tr>\n';
			}else{
				$ret .= '<tr><td colspan="2">'.$element["element"]->getOutput().'</td></tr>\n';
			}
		}
		$ret .= '</table>\n';
		return $ret;
	}
	
	/**
	 * Process the form elements and represents a form in paragraphs
	 * @return String
	 */
	public function getParagraphOutput(){
		$ret = "";
		foreach($this->elements as $element){
			if(!empty($element["label"])){
				$ret .= '<p>'.$element["label"].'</p>\n';				
			}
			$ret .= '<p>'.$element["element"]->getOutput().'</p>\n';
		}
		return $ret;
	}
}

?>